package edu.hubu.talentmanagementsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.CaseFormat;
import edu.hubu.talentmanagementsystem.common.QnUploadUtils;
import edu.hubu.talentmanagementsystem.mapper.FeedbackMapper;
import edu.hubu.talentmanagementsystem.mapper.context.MapperContext;
import edu.hubu.talentmanagementsystem.model.ext.Feedback;
import edu.hubu.talentmanagementsystem.service.IFeedbackService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import edu.hubu.talentmanagementsystem.common.EncodeOperation;
import edu.hubu.talentmanagementsystem.common.EncodeParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Optional;

/**
 * @author moonlan
 * date 2021/3/2 下午8:45
 */
@SuppressWarnings(value = "unused")
@Service
@Transactional(rollbackFor = Exception.class)
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements IFeedbackService {

    private final MapperContext context;
    private final QnUploadUtils qnUploadUtils;
    @Value("${qiniu.cdn.prefix}")
    private String prefix;

    public FeedbackServiceImpl(MapperContext context,QnUploadUtils qnUploadUtils) {
        this.context = context;
        this.qnUploadUtils = qnUploadUtils;
    }

    @Override
    public boolean deleteById(Integer id) {
        return context.getFeedbackMapper().deleteById(id) > 0;
    }

    @Override
    @EncodeOperation
    public Optional<Feedback> insert(@EncodeParam Feedback record) {
        int insert = context.getFeedbackMapper().insert(record);
        if (insert > 0) {
            return Optional.of(context.getFeedbackMapper().selectById(record.primaryKeyValue()));
        }
        return Optional.empty();
    }

    @Override
    public Optional<Feedback> selectById(Integer id) {
        QueryWrapper<Feedback> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<Feedback> lambdaQueryWrapper = getColumnWrapper(wrapper.lambda()).eq(Feedback::getFeedbackId, id);
        return Optional.of(context.getFeedbackMapper().selectOne(lambdaQueryWrapper));
    }

    @Override
    public List<Feedback> selectAll(String order) {
        QueryWrapper<Feedback> wrapper = new QueryWrapper<>();
        if (!StringUtils.hasText(order)) {
            LambdaQueryWrapper<Feedback> select = getColumnWrapper(wrapper.lambda());
            return context.getFeedbackMapper().selectList(select);
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        LambdaQueryWrapper<Feedback> select = getColumnWrapper(wrapper.lambda());
        return context.getFeedbackMapper().selectList(select);
    }

    @Override
    public Page<Feedback> selectAllPaged( Integer currentPage,
             Integer pageSize, String order) {
        QueryWrapper<Feedback> wrapper = new QueryWrapper<>();
        Page<Feedback> page = new Page<>(currentPage, pageSize);
        if (!StringUtils.hasText(order)) {
            LambdaQueryWrapper<Feedback> select = getColumnWrapper(wrapper.lambda());
            return context.getFeedbackMapper().selectPage(page, select);
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        LambdaQueryWrapper<Feedback> select = getColumnWrapper(wrapper.lambda());
        return context.getFeedbackMapper().selectPage(page, select);
    }

    private LambdaQueryWrapper<Feedback> getColumnWrapper(LambdaQueryWrapper<Feedback> wrapper) {
        return wrapper.select(
                Feedback::getFeedbackTypeId
                    ,
                Feedback::getFeedbackType
                    ,
                Feedback::getFeedbackId
                    ,
                Feedback::getFeedbackContentText
                    ,
                Feedback::getFeedbackContentImgUrl1
                    ,
                Feedback::getFeedbackContentImgUrl2
                    ,
                Feedback::getFeedbackContentImgUrl3
        );
    }

    @Override
    public Page<Feedback> selectLikePaged( Integer currentPage,
             Integer pageSize, Feedback record, String order) {
        Page<Feedback> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Feedback> wrapper = new QueryWrapper<>();
        if (!StringUtils.hasText(order)) {
            return context.getFeedbackMapper().selectPage(page, getColumnWrapper(getQueryWrapper(wrapper, record)));
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        return context.getFeedbackMapper().selectPage(page, getColumnWrapper(getQueryWrapper(wrapper, record)));
    }

    private LambdaQueryWrapper<Feedback> getQueryWrapper(QueryWrapper<Feedback>wrapper, Feedback record) {
        LambdaQueryWrapper<Feedback> lambda = wrapper.lambda();
        if (record.getFeedbackTypeId() != null) {
            lambda = lambda.eq(Feedback::getFeedbackTypeId,record.getFeedbackTypeId());
        }
        if (record.getFeedbackType() != null) {
            lambda = lambda.eq(Feedback::getFeedbackType,record.getFeedbackType());
        }
        if (record.getFeedbackId() != null) {
            lambda = lambda.eq(Feedback::getFeedbackId,record.getFeedbackId());
        }
        if (record.getFeedbackContentText() != null) {
            lambda = lambda.like(Feedback::getFeedbackContentText,record.getFeedbackContentText ());
        }
        if (record.getFeedbackContentImgUrl1() != null) {
            lambda = lambda.like(Feedback::getFeedbackContentImgUrl1,record.getFeedbackContentImgUrl1 ());
        }
        if (record.getFeedbackContentImgUrl2() != null) {
            lambda = lambda.like(Feedback::getFeedbackContentImgUrl2,record.getFeedbackContentImgUrl2 ());
        }
        if (record.getFeedbackContentImgUrl3() != null) {
            lambda = lambda.like(Feedback::getFeedbackContentImgUrl3,record.getFeedbackContentImgUrl3 ());
        }
        return lambda;
    }

    public int count(Feedback record) {
        return context.getFeedbackMapper().selectCount(getQueryWrapper(new QueryWrapper<>(), record));
    }

    @Override
    @EncodeOperation
    public Optional<Feedback> updateById(Integer id, @EncodeParam Feedback record) {
        record.setFeedbackId(id);
        int update = context.getFeedbackMapper().updateById(record);
        if (update > 0) {
            QueryWrapper<Feedback> wrapper = new QueryWrapper<>();
            LambdaQueryWrapper<Feedback> lambdaQueryWrapper = getColumnWrapper(wrapper.lambda()).eq(Feedback::getFeedbackId, id);
            return Optional.of(context.getFeedbackMapper().selectOne(getColumnWrapper(lambdaQueryWrapper)));
        }

        return Optional.empty();
    }

    @Override
    public boolean commitFeedback(String content, MultipartFile file1, MultipartFile file2, MultipartFile file3) {
        Feedback feedback = new Feedback();
        feedback.setFeedbackContentText(content);

        if (file1 != null){
            String url1 = qnUploadUtils.upload(file1);
            feedback.setFeedbackContentImgUrl1(url1);
        }
        if (file2 != null){
            String url2 = qnUploadUtils.upload(file2);
            feedback.setFeedbackContentImgUrl1(url2);
        }
        if (file3 != null){
            String url3 = qnUploadUtils.upload(file3);
            feedback.setFeedbackContentImgUrl1(url3);
        }
        return context.getFeedbackMapper().insert(feedback) > 0;
    }



}
